package com.wantupai.translate.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wantupai.translate.Constant;
import com.wantupai.translate.R;
import com.wantupai.translate.base.BaseActivity;
import com.wantupai.translate.bean.DataBean;
import com.wantupai.translate.bean.PhoneLoginBean;
import com.wantupai.translate.utils.CommonUtils;
import com.wantupai.translate.utils.NetUtils;
import com.wantupai.translate.utils.NetworkUtils;
import com.wantupai.translate.utils.PrefUtils;
import com.wantupai.translate.utils.ToastUtils;
import com.wantupai.translate.utils.VerifyUtil;
import com.wantupai.translate.utils.WindowUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author luxuchang
 * @date 2019/4/29 0029
 * @description
 */
public class LoginActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_verify_code)
    EditText etVerifyCode;
    @BindView(R.id.btn_get_verify_code)
    TextView btnGetVerifyCode;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initViews() {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        setTitle("登录");
//        hideLeftBackBtn();
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void processClick(View v) {

    }

    @OnClick({R.id.btn_get_verify_code, R.id.btn_login})
    public void onViewClicked(View view) {
        String phone = etPhone.getText().toString().trim();

        switch (view.getId()) {
            case R.id.btn_get_verify_code:
                if (TextUtils.isEmpty(phone)) {
                    ToastUtils.show("请输入手机号");
                    return;
                }

                if (!VerifyUtil.isMobileNO(phone)) {
                    ToastUtils.show("请输入正确的手机号");
                    return;
                } else {
                    sendSms(phone);
                }

                break;
            case R.id.btn_login:
                String verifyCode = etVerifyCode.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtils.show("请输入手机号");
                    return;
                }
                if (TextUtils.isEmpty(verifyCode)) {
                    ToastUtils.show("请输入验证码"+verifyCode);
                    return;
                }
                phoneLogin(phone, verifyCode);
                break;
        }
    }


    private void phoneLogin(String phoneNum, String code) {
        Map<String, String> maps = new HashMap<>();
        maps.put("mobile", phoneNum);
        maps.put("verification_code", code);
        maps.put("device_id", WindowUtils.getDeviceId(this));
        String model = WindowUtils.getSystemModel();
        maps.put("phone_model", TextUtils.isEmpty(model) ? "Android" : model);
        maps.put("phone_system", "Android");
        maps.put("jp_id", "");
        int NetType = NetworkUtils.getNetWorkType(this);
        if (NetType == 0) {
            Toast.makeText(this, "没有连接网络", Toast.LENGTH_SHORT).show();
        } else {
            NetUtils.getInstance().post(Constant.SSP_API_PHONE_LOGIN, maps, false, PhoneLoginBean.class, 0, new NetUtils.NetResponseListener() {
                @Override
                public void onSuccess(DataBean result, int requestCode) {
                    try {
                        PhoneLoginBean bean = (PhoneLoginBean) result.getData();
                        PrefUtils.setString(Constant.TPUID, bean.getUid());

                    } catch (Exception e) {
                        ToastUtils.show("登录失败，请重新登录");
                    }

                }

                @Override
                public void onFailed(int requestCode, String info) {
                    ToastUtils.show(info);
                }

            });
        }
    }

    private void sendSms(final String phoneNum) {
        Map<String, String> maps = new HashMap<>();
        maps.put("phone", phoneNum);
        int NetType = NetworkUtils.getNetWorkType(this);
        if (NetType == 0) {
            Toast.makeText(this, "没有连接网络", Toast.LENGTH_SHORT).show();
        } else {
            NetUtils.getInstance().post(Constant.SSP_API_SEND_SMS, maps, false, null, 0, new NetUtils.NetResponseListener() {
                @Override
                public void onSuccess(DataBean result, int requestCode) {
                    ToastUtils.show("发送成功");
                    CommonUtils.startCountDown(btnGetVerifyCode, R.color.black, R.color.red, 60000, "点击获取验证码");
                }

                @Override
                public void onFailed(int requestCode, String info) {
                    ToastUtils.show(info);
                }
            });
        }

    }

}
